<template>
  <div class="goods-container">
    <!-- 面包屑导航 -->
    <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>-->
    <bread :second="'商品列表'" :third="'商品管理'"></bread>
    <!-- 卡片 -->
    <el-card class="box-card">
      <div style="margin-top: 15px;">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input placeholder="请输入内容" v-model="userObj.query" class="iptBox">
              <el-button slot="append" icon="el-icon-search" @click="getGoods()"></el-button>
            </el-input>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" @click="addGoods">添加商品</el-button>
          </el-col>
        </el-row>
      </div>
      <!-- 表格 -->
      <el-table :data="tableData" border="" style="width: 100%;margin:20px 0">
        <el-table-column type="index" width="50" label="#"></el-table-column>
        <el-table-column prop="goods_name" label="商品名称" width="180"></el-table-column>
        <el-table-column prop="goods_price" label="商品价格" width="180"></el-table-column>
        <el-table-column prop="goods_weight" label="商品重量"></el-table-column>
        <el-table-column prop="add_time" label="创建时间"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              icon="el-icon-edit"
              @click="handleEdit(scope.row)"
            ></el-button>
            <el-button
              icon="el-icon-delete"
              size="mini"
              type="danger"
              @click="handleDelete(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="userObj.pagenum"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="userObj.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 编辑用户的表单对话框 -->
    <el-dialog title="修改用户" :visible.sync="dialogFormVisible">
      <el-form ref="formEditDialog" :model="formEdit" :rules="rulesFormEdit">
        <el-form-item label="用户名" :label-width="formLabelWidth">
          <el-input :disabled="true" v-model="formEdit.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮箱" :label-width="formLabelWidth">
          <el-input v-model="formEdit.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item prop="mobile" label="手机" :label-width="formLabelWidth">
          <el-input v-model="formEdit.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
           
<script>
import { goodrs_api, goodsDelete_api, usersEdit_api } from '@/api'
export default {
  data() {
    return {
      tableData: [],
      total: 1,

      /* 分页中的当前页码数 */

      /* 获取数据列表参数对象 */
      userObj: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },

      /* 对话框数据 */
      dialogFormVisible: false,
      formEdit: {},
      formLabelWidth: '120px',
      //  表单校验规则
      rulesFormEdit: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
          //   { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        mobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getGoods()
  },
  methods: {
    /* 分页组件提供的事件 */
    handleSizeChange(val) {
      //console.log(`每页 ${val} 条`)
      this.userObj.pagesize = val
      this.getGoods()
    },
    handleCurrentChange(val) {
      //console.log(`当前页: ${val}`)
      //  将获取的当前页赋值给  pagenum
      this.userObj.pagenum = val
      this.getGoods()
    },

    //  操作中的按钮
    handleEdit(row) {
      //console.log(row, 6677)
      this.formEdit = row
      this.dialogFormVisible = true
    },
    /* 点击编辑按钮中的确认按钮 */
    editForm() {
      this.$refs.formEditDialog.validate(async vaide => {
        //console.log(vaide,5555556666);
        if (vaide) {
          console.log(this.formEdit, 8899)
          const { data: res } = await usersEdit_api(this.formEdit)
          this.dialogFormVisible = false
          //  重新渲染最新表格数据
          this.getGoods()
        } else {
          return
        }
      })
    },
    /* 删除用户 */
    async handleDelete(id) {
      //console.log(id, 444)

      const res2 = await this.$confirm(
        '此操作将永久删除该文件, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
      console.log(res2, 88899)
      if (res2 == 'confirm') {
        const { data: res } = await goodsDelete_api(id)
        // console.log(res, 4455)

        //  重新渲染最新表格数据
        this.getGoods()
      }
    },

    /* 添加用户 */
    addGoods() {
      this.$router.push('/params/add')
    },

    /* 获取用户数据列表 */
    async getGoods() {
      const { data: res } = await goodrs_api(this.userObj)
      // console.log(res, 888)
      //  将获取的用户数据赋值给tableData
      this.tableData = res.data.goods
      this.total = res.data.total
    }
  }
}
</script>
<style scoped lang='less'>
.goods-container {
  .el-breadcrumb {
    margin-bottom: 20px;
  }

  .box-card {
    .iptBox {
    }
  }
}
</style>